package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 * 收缩版adapter
 */
public class SearchPeiXunShrinkRvAdapter extends RecyclerView.Adapter<SearchPeiXunShrinkRvAdapter.SearchPeiXunRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> list;
    private static int itemCount = 2;
    private SearchHomePagePeiXunListener listener;

    public SearchHomePagePeiXunListener getListener() {
        return listener;
    }

    public void setListener(SearchHomePagePeiXunListener listener) {
        this.listener = listener;
    }

    public SearchPeiXunShrinkRvAdapter(Context context, List<GoodsSortSearchAllBzBean.DataBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    public void addItemCount(int num) {
        itemCount = num;
    }

    @NonNull
    @Override
    public SearchPeiXunRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.item_cultivate, viewGroup, false);
        return new SearchPeiXunRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchPeiXunRvViewHolder holder, int i) {
        holder.tv_title.setText(list.get(i).getTitle());
        holder.tv_chubanshe.setText(list.get(i).getTypeName());
        holder.tv_price.setText("¥"+list.get(i).getPrice());
        holder.tv_author.setText(list.get(i).getAttrShow());
        Glide.with(context).load(list.get(i).getImg()).placeholder(R.mipmap.doc_default).into(holder.cultivate_Item1_Img);
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastTools.showToast(context, "当前URL为空");
                listener.searchPeiXunClick(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list.size() > 2)
            return itemCount;
        else
            return list.size();
    }

    class SearchPeiXunRvViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cultivate_Item1_Img)
        ImageView cultivate_Item1_Img;
        @BindView(R.id.cultivate_Item1_Author)
        TextView tv_author;
        @BindView(R.id.cultivate_Item1_ChuBanShe)
        TextView tv_chubanshe;
        @BindView(R.id.cultivate_Item1_Price)
        TextView tv_price;
        @BindView(R.id.cultivate_Item1_Title)
        TextView tv_title;
        @BindView(R.id.cultivate_Item1_LinearLayout)
        LinearLayout linearLayout;
        public SearchPeiXunRvViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public interface SearchHomePagePeiXunListener {
        void searchPeiXunClick(int index);
    }
}
